$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/bteaminfo/list',
        datatype: "json",
        colModel: [			
			{ label: '团队ID', name: 'id', index: 'id', align: "center", width: 60, key: true },
			{ label: '团队名称', name: 'teamName', index: 'team_name', align: "center", width: 100 },
			{ label: '创建人ID', name: 'userId', index: 'user_id', align: "center", width: 60 },
			{ label: '公众号 ', name: 'gzhName', index: 'gzh_name', align: "center", width: 60 },
			{ label: '展示状态', name: 'teamFlag', index: 'team_flag', align: "center", width: 60, formatter: function(value, options, row){
					return value === 0 ?
						'<span class="label label-danger">停用</span>' :
						'<span class="label label-success">正常</span>';
				}
			},
			{ label: '团队分享次数 ', name: 'shareNum', index: 'share_num', align: "center", width: 60 },
			{ label: '创建时间 ', name: 'createTime', index: 'create_time', align: "center", width: 80 },
			{ label: '更新时间 ', name: 'updateTime', index: 'update_time', align: "center", width: 80 },
			{ label: '备注 ', name: 'teamRemark', index: 'team_remark', align: "center", width: 100 }
        ],
		viewrecords: true,
        height: 520,
        rowNum: 15,
		rowList : [15,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
		postData:{"gzhCode":vm.q.gzhCode,"teamName":vm.q.teamName},
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        },
		beforeRequest: function() {
        	$("thead th").css("text-align","center");
			$("#jqgh_jqGrid_cb").css("text-align","left");// 表头checkbox居左
		}
    });
	$.ajax({
		type: "POST",
		async:false,
		url: baseURL + 'sys/buserinfo/getUserGzhList',
		contentType: "application/json",
		success: function(r){
			if(r.code==0){
				//vm.bbrandinfo=r.brandInfo;
				$("#selectUserGzhList").append(
					"<option value=''>全部平台</option>");
				for(var i=0;i<r.userGzhList.length;i++){
					$("#selectUserGzhList").append(
						"<option value=" + r.userGzhList[i].gzhCode +">"+ r.userGzhList[i].gzhName+"</option>");
				}
			}
		}
	});
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
			gzhCode: '',
			teamName: null
		},
		showList: true,
		title: null,
		bteaminfo: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		update: function () {
			var teamId = getSelectedRow();
			if(teamId == null){
				return ;
			}
			
			$.get(baseURL + "sys/bteaminfo/info/"+teamId, function(r){
				vm.showList = false;
                vm.title = "修改";
				vm.bteaminfo = r.bteaminfo;
			});
		},
		saveOrUpdate: function (event) {
			var url = "sys/bteaminfo/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.bteaminfo),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var teamIds = getSelectedRows();
			if(teamIds == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/bteaminfo/delete",
                    contentType: "application/json",
				    data: JSON.stringify(teamIds),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								vm.reload();
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                postData:{"gzhCode": vm.q.gzhCode,"teamName": vm.q.teamName},
                page:page 
            }).trigger("reloadGrid");
		}
	}
});